package com.asu.dltalk.main;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.jface.viewers.TreeViewer;

import com.asu.dltalk.model.GroupEntity;
import com.asu.dltalk.model.ITreeEntry;
import com.asu.dltalk.model.UserEntity;
import com.asu.dltalk.test.DataFactory;

public class DLtalkmain {

	protected Shell shell;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			DLtalkmain window = new DLtalkmain();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(338, 475);
		shell.setText("SWT Application");
		
		final ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
		toolBar.setBounds(0, 0, 322, 31);
		
		final ToolItem tltmFile = new ToolItem(toolBar, SWT.DROP_DOWN);
		tltmFile.setText("File");
		final Menu menu = new Menu(shell, SWT.POP_UP);
		
		MenuItem menuFile = new MenuItem(menu, SWT.PUSH);
		menuFile.setText("test");
		tltmFile.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event event){
				if (event.detail == SWT.ARROW) {
					Rectangle rect = tltmFile.getBounds();
					Point pt = new Point(rect.x, rect.y + rect.height);
					pt = toolBar.toDisplay(pt);
					menu.setLocation(pt.x, pt.y);
					menu.setVisible(true);
				}
			}
		});

		
		TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
		tabFolder.setBounds(10, 37, 302, 390);
		
		TabItem tbtmRoster = new TabItem(tabFolder, SWT.NONE);
		tbtmRoster.setText("Roster");
		
		Composite composite_1 = new Composite(tabFolder, SWT.NONE);
		tbtmRoster.setControl(composite_1);
		
		final TreeViewer tv = new TreeViewer(composite_1, SWT.BORDER);
		
		
		
		
		
		tv.setContentProvider(new RosterTreeContentProvider());
		tv.setLabelProvider(new RosterTreeLabelProvider());
		tv.setInput("root");
		
		final Tree roster = tv.getTree();
		roster.setBounds(10, 10, 274, 316);
		roster.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		roster.addListener(SWT.MouseDoubleClick, new Listener(){

			public void handleEvent(Event event) {
				Point point = new Point(event.x, event.y);
				TreeItem treeItem = roster.getItem(point);
				Object item = treeItem.getData();
			
				if(item instanceof GroupEntity){
					if(tv.getExpandedState(item)){
						tv.collapseToLevel(item, -1);		
					}else{
						tv.expandToLevel(item, 1);
					}					
				}
				if(item instanceof UserEntity){
					IStructuredSelection selection = (IStructuredSelection)tv.getSelection();
					ITreeEntry obj =(ITreeEntry)(selection.getFirstElement());
					TalkWindow tw = new TalkWindow(obj.getName());
					
				}
				
			}
			
		});
		
		Object inputobj = DataFactory.createTreeData();
		
		TreeEntryActionGroup actionGroup = new TreeEntryActionGroup(tv);
		actionGroup.fillContextMenu(new MenuManager());		

		tv.setInput(inputobj);
		
	}
}
